﻿#include "mainsystem.h"
#include "ui_mainsystem.h"
#include <QApplication>
#include <QMessageBox>
#include <QSqlError>
#include <QTextCodec>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>

mainsystem::mainsystem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mainsystem)
{
    ui->setupUi(this);
    //creatConnect();
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setPort(3306);
    db.setDatabaseName("stuinfo");
    db.setUserName("root");
    db.setPassword("root");

    bool ok = db.open();//建立数据库连接
    if(!ok)
    {
        QMessageBox::critical(nullptr,QObject::tr("false"),db.lastError().text());
        return;
    }
    else
    {
        QMessageBox::information(nullptr,QObject::tr("Tips"),QObject::tr("successed"));
        return;
    }
}

mainsystem::~mainsystem()
{
    delete ui;
}


void mainsystem::on_btnAdd_clicked()
{
    ui->showText->setText("");
    if(ui->stuId->text()==nullptr
            ||ui->stuName->text()==nullptr||ui->stuMajor->text()==nullptr)
    {
     QMessageBox::information(nullptr, "输入数据", "输入数据为空");
     ui->stuId->setText("");
     ui->stuName->setText("");
     ui->stuMajor->setText("");
    }
    else
    {
     QString str = QString("insert into stuInfo values('%1', '%2', '%3')")
             .arg(ui->stuId->text().toInt()).arg(ui->stuMajor->text()).arg(ui->stuName->text());
     QSqlQuery query;
     query.exec(str); //执行插入操作
     ui->stuId->setText("");
     ui->stuName->setText("");
     ui->stuMajor->setText("");
     QMessageBox::information(nullptr, "输入数据", "输入数据成功！");
    }
}

void mainsystem::on_btnDelete_clicked()
{
    ui->showText->setText("");
    //QMessageBox::information(nullptr, "输入学号", "请认真输入学号！");
    QString delId = ui->stuId->text();
    if( delId==nullptr )
    {
        QMessageBox::information(nullptr, "学号为空", "请重新输入！");
       // ui->stuId->setText("");
    }
    else {
        QSqlQuery query;
        bool flag = false;
        query.exec("select * from stuInfo;");   // 选择要遍历的内容，放在query1对象中
        while(query.next()) // query.next()代表将内容一行一行的遍历
        {
            if(query.value(0).toInt()==ui->stuId->text().toInt())
            {
                flag = true;
                QMessageBox::information(nullptr, "找到数据", "查找到该数据");
                QString str = QString("delete from stuInfo where id=('%1')")
                        .arg(ui->stuId->text().toInt());
                QSqlQuery query_del;
                query_del.exec(str); //执行插入操作
                ui->stuId->setText("");
                ui->stuName->setText("");
                ui->stuMajor->setText("");
                QMessageBox::information(nullptr, "删除数据", "删除数据成功！");
            }
        }
        if ( flag == false )
        {
            QMessageBox::information(nullptr, "查找数据", "查找失败，请重新输入！");
            ui->stuId->setText("");
        }
    }
}

void mainsystem::on_btnQuery_clicked()
{
    ui->showText->setText("");
    //QMessageBox::information(nullptr, "输入学号", "请认真输入学号！");
    QString delId = ui->stuId->text();
    if( delId==nullptr )
    {
        QMessageBox::information(nullptr, "学号为空", "请重新输入！");
        //ui->stuId->clear();
    }
    else {
        QSqlQuery query;
        bool flag = false;
        query.exec("select * from stuInfo;");   // 选择要遍历的内容，放在query1对象中
        while(query.next()) // query.next()代表将内容一行一行的遍历
        {
            if(query.value(0).toInt()==ui->stuId->text().toInt())
            {
                flag = true;
                QMessageBox::information(nullptr, "找到数据", "查找到该数据");
            }
        }
        if ( flag == false )
        {
            QMessageBox::information(nullptr, "查找数据", "查找失败，请重新输入！");
            ui->stuId->setText("");
        }
    }
}

void mainsystem::on_btnModify_clicked()
{
    ui->showText->setText("");
    //QMessageBox::information(nullptr, "输入学号", "请认真输入学号！");
    QString delId = ui->stuId->text();
    if( delId==nullptr )
    {
        QMessageBox::information(nullptr, "学号为空", "请重新输入！");
        //ui->stuId->setText("");
    }
    else {
        QSqlQuery query;
        bool flag = false;
        query.exec("select * from stuinfo;");   // 选择要遍历的内容，放在query1对象中
        while(query.next()) // query.next()代表将内容一行一行的遍历
        {
            if(query.value(0).toInt()==ui->stuId->text().toInt())
            {
                flag = true;
                QMessageBox::information(nullptr, "找到数据", "查找到该数据");
                QString str = QString("update stuinfo set major=('%1'),name=('%2') where id=('%3')")
                        .arg(ui->stuMajor->text()).arg(ui->stuName->text()).arg(ui->stuId->text().toInt());
                QSqlQuery query_del;
                query_del.exec(str); //执行插入操作
                ui->stuId->setText("");
                ui->stuName->setText("");
                ui->stuMajor->setText("");
                QMessageBox::information(nullptr, "修改数据", "修改数据成功！");
            }
        }
        if ( flag == false )
        {
            QMessageBox::information(nullptr, "查找数据", "查找失败，请重新输入！");
            ui->stuId->setText("");
        }
    }
}

void mainsystem::on_btnShow_clicked()
{
    QSqlQuery query;
    query.exec("select * from stuInfo;");   // 选择要遍历的内容，放在query1对象中
    ui->showText->setText("");
    while(query.next()) // query.next()代表将内容一行一行的遍历
    {
        // 打印每一行对应的字段 最好要转类型 不然很有可能回出错打印不成功
        // qDebug()<<query.value(0).toString()
        //      <<query.value(1).toInt();
        QString str = QString("学号：%1   专业：%2  姓名：%3").arg(query.value(0).toInt())
                .arg(query.value(1).toString()).arg(query.value(2).toString());
        ui->showText->append(str);
    }
}

void mainsystem::on_btnExit_clicked()
{
    this->close();
}

bool creatConnect()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setPort(3306);
    db.setDatabaseName("stuinfo");
    db.setUserName("root");
    db.setPassword("root");

    bool ok = db.open();//建立数据库连接
    if(!ok)
    {
        QMessageBox::critical(nullptr,QObject::tr("false"),db.lastError().text());
        return false;
    }
    else
    {
        QMessageBox::information(nullptr,QObject::tr("Tips"),QObject::tr("successed"));
        return true;
    }
}



